-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.py
More file actions
44 lines (37 loc) · 1 KB
/
Solution.py
File metadata and controls
44 lines (37 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, data):
if not self.head:
self.head = Node(data)
return
temp = self.head
while temp.next:
temp = temp.next
temp.next = Node(data)
def is_palindrome(self):
stack = []
temp = self.head
while temp:
stack.append(temp.data)
temp = temp.next
temp = self.head
while temp:
if temp.data != stack.pop():
return False
temp = temp.next
return True
if __name__ == "__main__":
ll = LinkedList()
n = int(input("Enter number of elements: "))
print("Enter elements:")
for _ in range(n):
ll.append(int(input()))
if ll.is_palindrome():
print("The linked list is a palindrome.")
else:
print("The linked list is not a palindrome.")